<%-- 
    Document   : DiaryPage
    Created on : 23.04.2011, 22:31:44
    Author     : ain
--%>

<%@page import="unc.pds.service.ServiceBeanRemote"%>
<%@page import="unc.pds.model.CommentRemote"%>
<%@page import="unc.pds.data.Consts"%>
<%@page import="unc.pds.util.ArchImpl"%>
<%@page import="unc.pds.util.Arch"%>
<%@page import="unc.pds.model.DiaryRemote"%>
<%@page import="unc.pds.service.GetterRemote"%>
<%@page import="unc.pds.model.UserRemote"%>
<%@page import="unc.pds.service.GetterRemoteHome"%>
<%@page import="javax.naming.InitialContext"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Diary</title>
    </head>
    <body>
        <%
            if(request.getParameter("id")!=null){
                ServiceBeanRemote sbr = (ServiceBeanRemote)session.getAttribute("service");
                DiaryRemote diary = (DiaryRemote)sbr.getByKey
                        (new Long(Long.parseLong(request.getParameter("id"))));
                out.println(diary.getCaption()+"<br>");
                out.println("POST: <br>");
                out.println(diary.getDiary()+"<br>");
                CommentRemote[] comments = diary.getComments();
                for (int i=0;i<comments.length;i++){
                    out.println("<h4>"+comments[i].getCaption()+"<br>");
                    out.println(comments[i].getComment()+"<br>");
                }
            }
        %>

        <form action="DiaryPage.jsp">
            <input type="text" name="post" value="post">
            <input type="text" name="caption" value="caption">
            <input type="text" name="id" value="<%out.print(request.getParameter("id"));%>">
            <input type="submit" name="newpost" value="newpost">
        </form>
        <%
            if(request.getParameter("newpost")!=null){
                ServiceBeanRemote sbr = (ServiceBeanRemote)session.getAttribute("service");
                Arch doc = new ArchImpl("root",null);
                DiaryRemote diary = (DiaryRemote)sbr.getByKey(new Long(Long.parseLong(request.getParameter("id"))));
                doc = new ArchImpl("root",null);
                doc.addChild(new ArchImpl(Consts.CONT__COMMENT_POST,request.getParameter("post")));
                doc.addChild(new ArchImpl(Consts.ATTR__COMMENT_CAPTION,request.getParameter("caption")));
                diary.addComment(doc);
                response.sendRedirect("DiaryPage.jsp?id="+diary.getKey());
            }
        %>
    </body>
</html>
